package com.jic.market.mapper;

import com.jic.market.entity.PromotionEntity;
import com.jic.market.entity.PromotionMerchantProductEntity;
import com.jic.market.entity.mutex.MutexProductEntity;
import com.jic.market.orm.BaseMapper;
import com.jic.market.orm.Pagination;
import com.jic.market.request.PromotionPolymericRequest;
import com.jic.market.request.rest.PresellProductRequest;
import com.jic.market.response.PresellProductResponse;
import com.jic.market.response.PromotionPolymericProductResponse;
import com.jic.market.response.PromotionSingleProductResponse;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Repository
public interface PromotionMerchantProductEntityMapper
        extends BaseMapper<PromotionMerchantProductEntity> {

    /**
     * 根据 促销id，与 商家商品id，更新 互斥标识
     *
     * @param promotionId 条件：促销id
     * @param mpId        条件：商家商品 id
     * @param mutexFlag   更新值：互斥标识 1表示互斥，0表示共享
     * @param updateTime  更新值：更新时间
     * @return
     */
    int updateCondition(@Param("promotionId") Long promotionId, @Param("mpId") Long mpId,
                        @Param("mutexFlag") Integer mutexFlag, @Param("updateTime") Date updateTime);


    /**
     * 根据促销 id 和商品 id 删除 数据
     *
     * @param promotionId
     * @param mpId
     * @return
     */
    int deleteEntityByPromotionIdAndMpId(
            @Param("promotionId") Long promotionId, @Param("mpId") Long mpId);

    /**
     * 通过id查询实体
     *
     * @param id
     * @return
     */
    PromotionMerchantProductEntity queryEntityById(@Param("id") Long id);


    /**
     * 根据促销 id 和商品 id 查询数据
     *
     * @param promotionId
     * @param mpId
     * @return
     */
    PromotionMerchantProductEntity queryEntityByPromotionIdAndMpId(
            @Param("promotionId") Long promotionId, @Param("mpId") Long mpId);

    /**
     * 根据促销 主键 更新
     *
     * @param promotionId
     * @param mpId
     * @return
     */
    Integer updateEntityByPromotionIdAndMpId(@Param("promPrice") BigDecimal promPrice,
                                             @Param("promotionId") Long promotionId, @Param("mpId") Long mpId);

    /**
     * 批量插入
     *
     * @param list
     */
    void insertBatch(@Param("list") List<PromotionMerchantProductEntity> list);

    /**
     * 根据促销 id 和商品 id 查询数据列表
     *
     * @param storeIds
     * @param mpId
     * @param mutexFlag MutexFlagEmum
     * @return
     */
    List<PromotionMerchantProductEntity> queryEntityListByStoreIdsAndMpId(
            @Param("storeIds") List<Long> storeIds,
            @Param("mpId") Long mpId,
            @Param("mutexFlag") Integer mutexFlag);


    /**
     * 根据促销 id 查询数据列表
     *
     * @param promotionMerchantProduct
     * @return
     */
    List<PromotionMerchantProductEntity> queryPromotionMerchantProduct(
            @Param("promotionMerchantProduct") PromotionMerchantProductEntity promotionMerchantProduct);

    @Select("select count(*) from promotion_merchant_product where promotion_id = #{promotionId} and type_of_product = 20 and delete_flag = 0")
    Long skuCount(@Param("promotionId") Long promotionId);

    void deleteEntityByPromotionIdAndMerchantId(@Param("merchantId") Long merchantId, @Param("promotionId") Long promotionId);

    void deleteEntityByPromotionIdAndStoreId(@Param("storeId") Long storeId, @Param("promotionId") Long promotionId);

    void deleteEntityByPromotionIdAndMpIdAndTimeId(@Param("promotionId") Long promotionId, @Param("goodsId") String goodsId, @Param("skillTimeId") String timeId);

    void updateBySkillTimeId(PromotionMerchantProductEntity productEntity);

    void deleteEntityByPromotionIdAndTimeId(@Param("promotionId") Long promotionId, @Param("skillTimeId") String skillTimeId);

    @Update("update promotion_merchant_product set delete_flag = 1 where promotion_id = #{promotionId}")
    void deleteEntityByPromotionId(@Param("promotionId") Long promotionId);

    /**
     * 根据产品id获取有效的活动类型
     *
     * @param mpId
     * @return
     */
    Set<MutexProductEntity> getIngFrontPromTypes(@Param("promotionId") Long promotionId, @Param("mpId") Long mpId, @Param("promType") Integer promType);

    Set<MutexProductEntity> getIngPayFrontPromTypes(@Param("promotionId") Long promotionId, @Param("mpId") Long mpId, @Param("promType") Integer promType);

    @Select("select a.mp_id from promotion_merchant_product a where FIND_IN_SET(a.id,#{ids})")
    List<Long> queryEntityListByIds(@Param("ids") String ids);

    @Update("UPDATE promotion_merchant_product SET mutex_flag = 0 WHERE FIND_IN_SET(id,#{ids})")
    void updateMutexByIds(@Param("ids") String ids);

    @Update({
            "<script>",
            "UPDATE promotion_merchant_product SET mutex_flag = 0 WHERE promotion_id = #{promotionId} and FIND_IN_SET(mp_id,#{mpIds})",
            "<if test='skillTimeId'> and skill_time_id = #{skillTimeId}</if>",
            "</script>"
    })
    void updateNoMutexListByPromotionIdAndMpIdAndSkillTimeId(@Param("promotionId") Long promotionId, @Param("skillTimeId") String skillTimeId, @Param("mpIds") String mpIds);

    List<PromotionMerchantProductEntity> findMutexOfPage(@Param("po") PromotionMerchantProductEntity param, @Param("page") Pagination pagination);

    int countMutexOfPage(@Param("po") PromotionMerchantProductEntity param);

    List<PromotionMerchantProductEntity> findMutexOfPageV2(@Param("po") PromotionMerchantProductEntity param, @Param("page") Pagination pagination);

    int countMutexOfPageV2(@Param("po") PromotionMerchantProductEntity param);

    int updateCanNull(PromotionMerchantProductEntity param);


    int insertBatchNew(List<PromotionMerchantProductEntity> list);

    Integer queryEntityByPromotionIdAndMpIdAndSkillTimeId(@Param("promotionId") Long promotionId,@Param("mpId") Long mpId,@Param("timeId") String timeId);

    //void updateBatch(@Param("items")List<PromotionMerchantProductEntity> updateProductEntities);

    List<PromotionEntity> queryPromotionMerchant(@Param("mpId") Long mpId);

    PresellProductResponse queryPresellByMpId(PresellProductRequest request);

    List<PromotionPolymericProductResponse> queryPromotionPolyProductList(@Param("request") PromotionPolymericRequest request);


    PromotionSingleProductResponse querySingleByMpId(@Param("request") PromotionPolymericRequest request);


    List<PromotionSingleProductResponse> querySingleByMpIds(@Param("mpIds") List<Long> mpIds);
}
